﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Net;
using System.Windows.Forms;

namespace Resource
{
    namespace Elements
    {
        public class Element
        {
            private Element() { }

            /// <summary>
            /// 使用指定参数返回新的对象实例
            /// </summary>
            /// <param name="order">原子序数</param>
            /// <param name="name">元素名称</param>
            /// <param name="symbol">元素符号</param>
            /// <param name="relativeAtomicMass">相对原子质量</param>
            /// <param name="valence">化合价</param>
            public Element(string order,string name,string symbol,string relativeAtomicMass,string valence)
            {
                this.order = order;
                this.name = name;
                this.symbol = symbol;
                if (relativeAtomicMass.Equals("Empty"))
                {
                    //有些元素的相对原子质量未在书中指出
                    this.relativeAtomicMasAvailable = false;
                    this.relativeAtomicMass = "Empty";
                }
                else
                {
                    this.relativeAtomicMass = relativeAtomicMass;
                    this.relativeAtomicMasAvailable = true;
                }
                if (valence.Equals("Empty"))
                {
                    //有些元素的化合价未在书中指出
                    this.valenceAvailable = false;
                    this.valence = "Empty";
                }
                else
                {
                    this.valence = valence;
                    this.valenceCount = this.valence.Length;
                    this.valenceAvailable = true;
                }
            }

            string order;
            string name;
            string symbol;
            bool relativeAtomicMasAvailable;
            string relativeAtomicMass;
            private bool valenceAvailable;
            string valence;
            int valenceCount = 1;

            /// <summary>
            /// 原子序数
            /// </summary>
            public string Order { get { return order; } }

            /// <summary>
            /// 元素名称
            /// </summary>
            public string Name { get { return name; } }
            /// <summary>
            /// 元素符号
            /// </summary>
            public string Symbol { get { return symbol; } }
            /// <summary>
            /// 相对原子质量
            /// </summary>
            public string RelativeAtomicMass
            {
                get
                {
                    if (!relativeAtomicMasAvailable)
                        throw new NotSupportedException("不支持获取相对原子质量");
                    return relativeAtomicMass;
                }
            }
            /// <summary>
            /// 化合价
            /// </summary>
            public string Valence
            {
                get
                {
                    if (!valenceAvailable)
                        throw new NotSupportedException("不支持获取化合价");
                    return valence;
                }
            }

            public override string ToString()
            {
                return string.Format("{0}.{1}[{2}] RelativeAtomicMass:{3} Valence:{4}", order, name, symbol, relativeAtomicMass, valence);
            }
        }

        public class XElements
        {
            private Element[] elements;
            private bool isElementsLoaded = false;

            public bool IsExist(string uri)
            {
                HttpWebRequest req = null;
                HttpWebResponse res = null;
                try
                {
                    req = (HttpWebRequest)WebRequest.Create(uri);
                    req.Method = "HEAD";
                    req.Timeout = 3000;
                    res = (HttpWebResponse)req.GetResponse();
                    return (res.StatusCode == HttpStatusCode.OK);
                }
                catch
                {
                    return false;
                }
                finally
                {
                    if (res != null)
                    {
                        res.Close();
                        res = null;
                    }
                    if (req != null)
                    {
                        req.Abort();
                        req = null;
                    }
                }
            }


            public Element[] Elements
            {
                get
                {
                    if (!isElementsLoaded)
                    {
                        XmlDocument xDoc = new XmlDocument();
                        string url;
                        if (IsExist(@"http://xc-boy.xicp.net/resources/Elements.xml"))
                            url = @"http://xc-boy.xicp.net/resources/Elements.xml";
                        else
                            url = @"http://x.libdd.com/farm1/ce518c/9a72d4ef/Elements.xml";
                        xDoc.Load(url);
                        XmlElement xElement = xDoc.DocumentElement;
                        XmlNodeList xNodeList = xElement.ChildNodes;
                        elements = new Element[xNodeList.Count];
                        int i = 0;
                        foreach (XmlNode x in xNodeList)
                        {
                            elements[i] = new Element(x.SelectSingleNode("Order").InnerText,
                                x.SelectSingleNode("Name").InnerText,
                                x.SelectSingleNode("Symbol").InnerText,
                                x.SelectSingleNode("RelativeAtomicMass").InnerText,
                                x.SelectSingleNode("Valence").InnerText);
                            i++;
                        }
                    }
                    return elements;
                }
            }

        }
    }
}
